<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.2.1/axios.js"></script>
    <style>
        .loading{
            width: 100%;
            height: 3000px;
            background: rgba(0,0,0,0.5);
            display: none;
        }

        img{
            width: 100px;
            height: 100px;
            position:absolute;
            top: 50%;
            left: 50%;
            margin: -50px 0 0 -50px;
        }
    </style>
</head>

<body>
    <button>获取数据</button>
    <div class="loading"><img src="./img/1.gif" alt=""></div>
    <script>
        // 获取ul和按钮
        let btns = document.querySelectorAll('button');
        let loading = document.querySelector('.loading')
        
        // 1.设置请求拦截器 - 请求发送之前执行
        btns[0].onclick =async function(){
            let res = await axios({
                url:"http://localhost:3000/movies"
            });
            console.log(res);
        }
        // 拦截 - 响应
        axios.interceptors.response.use(res=>{
            console.log("响应拦截器执行");
            loading.style.display = 'none';
            return res.data;
        })
        // 拦截 - 请求
        axios.interceptors.request.use(config=>{
            console.log("请求拦截器执行");
            // 显示loading
            loading.style.display = 'block';
            return config;
        })
       
    </script>
</body>

</html>